<?php

class PhpFox_Mod_Account_ComponentInviteImport extends BaseComponent
{
    /** Processing method
     */
	function process()
	{	
		if ( !extension_loaded('curl') )
		{
			return (App::getParam('debug') ? 'PHP CURL is not installed' : '');
		}

		if ( !file_exists(App::getParam('path') . 'plugins/importer/index.php') )
		{
			return (App::getParam('debug') ? 'Plugin is not installed: plugins/importer/index.php' : '');
		}
		
		App::loadClass('file/File');
		$oFile = new CFile;
		if (!$oFile->isWritable(App::getParam('path') . 'plugins/importer/'))
		{
			return (App::getParam('debug') ? 'Plugin folder is not writable: plugins/importer/' : '');
		}

		$oReq = &App::getRequest();
		$sAction = $this->getParam('action');

		if ( $aList = $oReq->get('list') )
		{
			$oSrvSec = &App::getModuleService('Account', 'Security');
			$oSrvInvite = &App::getModuleService('Account','Invite');
			$oAcc = $oSrvSec->getCurrentUser();
			$aSended = $oSrvInvite->sendInvites($aList, $oAcc, "");
		}

		if ( $sAction == 'invites' )
		{
			$aData = $this->oDatabase->getRows("SELECT c.c_name, c.c_email, u.user, u.email FROM ". App::getT('contacts') ." AS c ".
				" LEFT JOIN ". App::getT('user') ." AS u ON (c.c_email = u.email) ".
				" WHERE c.uid = '". App::getUser('id') ."'");

			$this->oDatabase->_delete(App::getT('contacts'), " uid = '". App::getUser('id') ."' ");
		}

		if ( $oReq->get('username') && $oReq->get('password') )
		{
			ob_start();
			ob_clean();

			$ch = curl_init(App::getParam('url') . 'plugins/importer/'. $oReq->get('client') .'.php');
			curl_setopt($ch, CURLOPT_HEADER, false);
			curl_setopt($ch, CURLOPT_POSTFIELDS, "&username=". $oReq->get('username') ."&password=". $oReq->get('password') ."");
			curl_exec($ch);
			curl_close($ch);

			$sContent = ob_get_contents();

			ob_clean();

			$aData = unserialize($sContent);

			$i = 0;
			foreach($aData as $email)
			{
				$i++;
				$iCheck = $this->oDatabase->getRow("SELECT COUNT(*) AS count FROM ". App::getT('contacts') ." WHERE c_email = '". $this->oDatabase->escape($email['contacts_email']) ."'");
				if ( $iCheck['count'] == 0 && ereg('@', $email['contacts_email']) )
				{
					$this->oDatabase->insert(App::getT('contacts'), array('uid' => App::getUser('id'), 'c_name' => $email['contacts_name'], 'c_email' => $email['contacts_email'], 'time' => time()));
				}
			}

			if ( $i )
			{
				App::gotoUrl('public.account.invite', array('view' => 'invites'));
			}
			else
			{
				App::gotoUrl('public.account.invite', array('view' => 'importer', 'client' => $oReq->get('client')), App::format('Mod_Account.failed_import'));
			}
		}

		$this->oTpl->assign(array
		(
			'importers' => App::getParam('Mod_Account.importers'),
			'iRowSize' => 3,
			'client' => $oReq->get('client'),
			'import' => $oReq->get('import'),
			'aData' => isset($aData) ? $aData : '',
			'iTotal' => isset($aData) ? count($aData) : '',
			'aList' => (!empty($aList) ? $aList : ''),
			'iList' => (!empty($aList) ? count($aList) : ''),
			'action' => $sAction
		));

		$sHtml = $this->oTpl->fetch('InviteImport.html');
		return $sHtml;
	}

}

?>